using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
/*------------------------------------------
 * 
 * 	 - Put GUITextures on separate layer
 *	 - Set minimapCamera to display that layer
 *	 - Put minimapCamera in cameraToUse
 * 
--------------------------------------------*/


//[RequireComponent (typeof (GUITexture))]
public class ObjectLabel : MonoBehaviour {
 
private Transform target;  // Object that this label should follow
private Vector3 offset = Vector3.up;    // Units in world space to offset; 1 unit above object by default
private bool clampToScreen = false;  // If true, label will be visible even if object is off screen
private float clampBorderSize = 0.05f;  // How much viewport space to leave at the borders when a label is being clamped
private bool useMainCamera = false;   // Use the camera tagged MainCamera
public Camera cameraToUse ;   // Only use this if useMainCamera is false
Camera cam ;
Transform thisTransform;
Transform camTransform;
//private GUITexture[] texturesToDisplay = new GUITexture[]{};
private List<float> texturesTimers = new List<float>();	
private List<Transform> objectTransforms = new List<Transform>();
private List<GameObject> texturesToDisplay = new List<GameObject>();
private List<Vector2> textureSizes	= new List<Vector2>();
//private float[] texturesTimers = new float[]{};	
//private Transform[] objectTransforms = new Transform[]{};

	void Start () 
    {
	thisTransform = transform;
    if (useMainCamera)
        cam = Camera.main;
    else
        cam = cameraToUse;
    camTransform = cam.transform;
	}
 
 
    void Update()
    {
 		for (int i = 0; i < texturesToDisplay.Count; i++){
			if (objectTransforms[i] == null)
				texturesTimers[i] = 1;
			else {
				target = objectTransforms[i];
				thisTransform = texturesToDisplay[i].transform;
		        if (clampToScreen)
		        {
		            Vector3 relativePosition = camTransform.InverseTransformPoint(target.position);
		            relativePosition.z =  Mathf.Max(relativePosition.z, 1.0f);
		            thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
		            thisTransform.position = new Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0f - clampBorderSize),
		                                             Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0f - clampBorderSize),
		                                             thisTransform.position.z);
		 
		        }
		        else
		        {
		            thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
		        }
				GUITexture texture = thisTransform.guiTexture;
	 			texture.pixelInset = new Rect(-((target.position.y/cam.transform.position.y)*textureSizes[i].x)/2,-((target.position.y/cam.transform.position.y)*textureSizes[i].y)/2, (target.position.y/cam.transform.position.y)*textureSizes[i].x, (target.position.y/cam.transform.position.y)*textureSizes[i].y);
				//thisTransform.position = new Vector3(thisTransform.position.x, 0.5f,thisTransform.position.y);
				//texturesToDisplay[i].guiTexture.pixelInset.height = objectTransforms[i].transform.position.y/cam.transform.position.y;
			}
		}
		checkTextures();
    }
	
	
	private void checkTextures(){
		for (int i = 0; i < texturesToDisplay.Count; i++){
			if (texturesTimers[i]<Time.time && texturesTimers[i] != 0){
				Destroy(texturesToDisplay[i].gameObject);
				//Destroy(objectTransforms[i].gameObject);
				texturesToDisplay.RemoveAt(i);
				texturesTimers.RemoveAt(i);
				objectTransforms.RemoveAt(i);
				textureSizes.RemoveAt(i);
			}
		}
	}
	
	public void displayTexture(GameObject textureObject, Transform transform, float Timer){
		if (!objectTransforms.Contains(transform)){
			GameObject newTexture = Instantiate(textureObject) as GameObject;
			texturesToDisplay.Add(newTexture);
			textureSizes.Add(new Vector2(newTexture.guiTexture.pixelInset.width,newTexture.guiTexture.pixelInset.height));
			if (Timer > 0)
				texturesTimers.Add(Timer+Time.time);
			else
				texturesTimers.Add(0);
			objectTransforms.Add(transform);
		}
	}	
		public void clearTextures(){
				for (int i = 0; i < texturesToDisplay.Count; i++){
					Destroy(texturesToDisplay[i].gameObject);
				}
				texturesToDisplay.Clear();
				objectTransforms.Clear();
				texturesTimers.Clear();
				textureSizes.Clear();
		}
	
	
}